package net.zdreamer.shunyi.ui.fragment;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import net.zdreamer.shunyi.R;


public class MessageFragment extends Fragment implements View.OnClickListener {

    private RelativeLayout titleitem;
    private TextView title;
    private LinearLayout privateletter, comment,letterbuttom,commentbuttom;
    private boolean ischecked=true;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_message, container, false);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onStart() {
        super.onStart();
        title = (TextView) getView().findViewById(R.id.title_content);
        titleitem = (RelativeLayout) getView().findViewById(R.id.title_item);
        titleitem.setBackgroundResource(R.drawable.newsbiaoti);
        title.setText("消息盒子");
        title.setTextColor(getResources().getColor(R.color.white));
        privateletter = (LinearLayout) getView().findViewById(R.id.privateleter);
        comment = (LinearLayout) getView().findViewById(R.id.comment);
        letterbuttom=(LinearLayout)getView().findViewById(R.id.letterbuttom);
        commentbuttom=(LinearLayout)getView().findViewById(R.id.commentbuttom);
        privateletter.setOnClickListener(this);
        comment.setOnClickListener(this);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        addFragment(new PrivateLetterFragment());
    }

    public void addFragment(Fragment fragment) {
        FragmentTransaction ft = getChildFragmentManager().beginTransaction();

        ft.replace(R.id.childfragment, fragment);
        ft.commit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.privateleter:
                ischecked=true;
                addFragment(new PrivateLetterFragment());
                setbackground();
                break;
            case R.id.comment:
                ischecked=false;
                addFragment(new CommentFragment());
                setbackground();
                break;
            default:
                break;
        }
    }
    public void setbackground(){
        if (ischecked){
            letterbuttom.setVisibility(View.VISIBLE);
            commentbuttom.setVisibility(View.INVISIBLE);
        }else{
           letterbuttom.setVisibility(View.INVISIBLE);
            commentbuttom.setVisibility(View.VISIBLE);
        }
    }
}
